package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.Jdbc;

/**
 * Servlet implementation class remove_stuclass
 */
@WebServlet("/remove_stuclass")
public class remove_stuclass extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public remove_stuclass() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String stuid = request.getParameter("stuid");
		String centername = request.getParameter("centername");
		String classname = request.getParameter("classname");
		String delete = "delete from  stu_class where stuid = '" + stuid + "' and classname='" + classname
				+ "' and centername='" + centername + "'";
		System.out.println(delete);
		// 退课的同时应该讲次节课的可选人数加一
		String update = "update class set stunumrest=stunumrest+1 where classname='" + classname + "' and centername='"
				+ centername + "'";
		Jdbc j1 = null;

		try {
			j1 = new Jdbc(delete);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Jdbc j2 = null;
		try {
			j2 = new Jdbc(update);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//最后将学生所在班级的签到表中的记录删除，都会保存在check_info表中
		String checkclass=centername+classname+"check";
		String delete1 = "delete from "+checkclass+" where stuid='"+stuid+"'";
		try {
			Jdbc j3 = new Jdbc(delete1);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (j1.count > 0) {
			request.getRequestDispatcher("showsuccess.jsp").forward(request, response);
		}
		
	}

}
